import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../tool/assets.dart';

class SearchItem extends StatefulWidget {
  final BLEPeripheral device;
  final String localName;
  bool ison;
  Function() onSelect;
  Function() onPressLongToChangeName;
  SearchItem(
      {super.key,
      required this.ison,
      required this.device,
      required this.localName,
      required this.onSelect,
      required this.onPressLongToChangeName});

  @override
  State<SearchItem> createState() => _SearchItemState();
}

class _SearchItemState extends State<SearchItem> {
  bool get isconncet {
    return widget.device.result.device.isConnected;
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constraints) {
      return GestureDetector(
          onLongPress: widget.onPressLongToChangeName,
          child: Container(
              width: constraints.maxWidth,
              height: 40,
              color: Colors.white,
              child: Stack(
                children: [
                  ///设备名字
                  Positioned(
                    left: 10,
                    top: 0,
                    bottom: 0,
                    child: Center(
                      child: Text(
                        widget.device.result.device.platformName,
                        style: TextStyle(fontSize: 13, color: widget.ison ? Color_select_btn : Colors.black),
                      ),
                    ),
                  ),

                  ///设备自定义名字
                  Positioned(
                    right: 70,
                    top: 0,
                    bottom: 0,
                    child: Center(
                      child: Text(
                        widget.localName,
                        textAlign: TextAlign.right,
                        style: TextStyle(fontSize: 13, color: widget.ison ? Color_select_btn : Colors.black),
                      ),
                    ),
                  ),

                  ///switch开关
                  Positioned(
                      right: 0,
                      top: 10,
                      bottom: 10,
                      child: Transform.scale(
                        scale: 0.8,
                        child: Switch(value: widget.ison, onChanged: (ison) => widget.onSelect()),
                      )),

                  ///下划线
                  Positioned(
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: SizedBox(
                      height: 0.5,
                      child: Container(color: Colors.black),
                    ),
                  ),
                ],
              )));
    });
  }
}
